<?php

namespace App\Http\Controllers\Course;

use App\Http\Controllers\BaseController;
use App\Models\CourseCategory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class CategoryController extends BaseController
{
    public function index()
    {

        //$staffUrl = 'https://qw-openapi-tx.dustess.com/customer/v1/getStaffList?accessToken='.$this->getAccessToken();
//        $result = Http::post($staffUrl,[
//            'page' =>1,
//            'page_size' => 50,
//            'phones' => '13619171079'
//        ])->json();
//
//        dd($result);

//        $result = Http::post('https://qw-openapi-tx.dustess.com/customer/v1/getCustomerList?accessToken='.$this->getAccessToken(),[
//                    'mobiles' => ['18509212669'],
//                    'page_size' => 1
//                ])->json();
//
//        if (count($result['data'])){
//            $customerId =current($result['data'])['id'];
//
//            dump($customerId);
//        }
//
//
//         $result = Http::post('https://qw-openapi-tx.dustess.com/customer/v1/batchAddCustomer?accessToken='.$this->getAccessToken(),[
//            'customer_list' => [
//                [
//                 'remark' => 'xiongxiong123',
//                 'mobiles' => [
//                     [
//                         'tel' => '18509212669',
//                         'type' => 'mobile',
//                         'display' => '手机号'
//                     ]
//                 ],
//                 'categoryId' => '00000000-0000-0000-0000-000000000000',
//                 'pool' => '232810069270495232'
//                ]
//             ]
//         ])->json();
//
//
//        dd(current($result['data']['fail'])['id']);


//        $result = Http::post('https://qw-openapi-tx.dustess.com/enterprise/v1/list?accessToken='.$this->getAccessToken(),[
//            'owner_ids' => ['woJawBCQAAkXw9IUDia0QJNaGAjxJ1ig']
//        ])->json();
//
//
//        dd($result);

       $data = CourseCategory::query()
                    ->where('status',1)
                    ->orderBy('sort')
                    ->orderByDesc('id')
                    ->get();

       return $this->success($data);
    }


    public  function getAccessToken()
    {
        if (Cache::has('access_token')){
            return Cache::get('access_token');
        }

        $result =  Http::post('https://qw-openapi-tx.dustess.com/auth/v1/access_token/token',[
            'clientId' => 'cfk65r8h3fzgc',
            'clientSecret' => 'Lef6GJ2vA4MRDl4kF28mfoGxP6Y5nG5s'
        ])->json();


        if ($result['code']==0){

            $time = strtotime($result['data']['expiresIn'])-time();
            Cache::set('access_token',$result['data']['accessToken'],$time);

            return  $result['data']['accessToken'];
        }

        return '';
    }
}
